package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.dto.ButtonFormDto;
import cn.livingCloud.dto.ButtonFormListDto;
import cn.livingCloud.entity.content.Button;
import cn.livingCloud.repository.ButtonMapper;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by sujiantang on 2016/1/7.
 */
public class ButtonFormListDtoLoader {
    private transient ButtonMapper mapper = BeanProvider.getBean(ButtonMapper.class);

    private ButtonFormListDto formListDto;

    public ButtonFormListDtoLoader(ButtonFormListDto formListDto) {
        this.formListDto = formListDto;
    }

    public void load() {
        String uuid = formListDto.getUuid();
        if (!StringUtils.isEmpty(uuid)) {
            loadButton();
        }
    }

    private void loadButton() {
        Button button = mapper.findButtonByUuid(formListDto.getUuid());
        formListDto.load(button);
        loadSubButton(button);
    }

    private void loadSubButton(Button button) {
        List<Button> allSubButton = mapper.findAllSubButton(button.getUuid());
        List<ButtonFormDto> buttonList = formListDto.getButtonList();
        for (Button subButton : allSubButton) {
            buttonList.add(new ButtonFormDto(subButton));
        }
    }
}
